package managedbean.FRM.CreditAccount;

import entity.CRM.Customer;
import entity.CRM.SaleLead;
import entity.CRM.SaleOrder;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.CustomerBeanLocal;
import session.stateless.CRM.SaleLeadBeanLocal;
import session.stateless.FRM.CreditAccountBeanLocal;


@ManagedBean(name="viewCreditAccountManagerBean")
@ViewScoped
public class ViewCreditAccountManagerBean implements Serializable {
    public ViewCreditAccountManagerBean() {}
    
    @EJB
    private CreditAccountBeanLocal creditAccountBeanLocal;
    @EJB
    private CustomerBeanLocal customerBeanLocal;
    @EJB
    private SaleLeadBeanLocal saleLeadBeanLocal;
    
    private Customer selectedCustomer;
    private SaleOrder selectedSaleOrder;
    private String saleLeadName;
    private boolean isExisting;
    private List<SaleOrder> pending;
    private List<SaleOrder> confirmed;
    
    public void initView(PhaseEvent event)
    {
        if (selectedCustomer!=null)
        {
            this.setIsExisting(true);
            this.setPending(selectedCustomer.getSaleOrder_Pending());
            this.setConfirmed(selectedCustomer.getSaleOrder_Confirmed());
        }
        else this.setIsExisting(false);
    }

    public List<SaleOrder> getConfirmed() {
        return confirmed;
    }

    public void setConfirmed(List<SaleOrder> confirmed) {
        this.confirmed = confirmed;
    }

    public List<SaleOrder> getPending() {
        return pending;
    }

    public void setPending(List<SaleOrder> pending) {
        this.pending = pending;
    }
    
    
    
    public boolean isIsExisting() {
        return isExisting;
    }

    public void setIsExisting(boolean isExisting) {
        this.isExisting = isExisting;
    }
    
    public Customer getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(Customer selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }
    
    
    public String getSaleLeadName() {
        return saleLeadName;
    }

    public SaleOrder getSelectedSaleOrder() {
        return selectedSaleOrder;
    }

    public void setSelectedSaleOrder(SaleOrder selectedSaleOrder) {
        this.selectedSaleOrder = selectedSaleOrder;
    }

    public void setSaleLeadName(String saleLeadName) {
        this.saleLeadName = saleLeadName;
    }
    
    
    public List<String> complete(String query)
    {
        List<String> results = new ArrayList<String>();  
        List<SaleLead> list = saleLeadBeanLocal.getAllSalesLead();
        List<Customer> list2 = customerBeanLocal.getAllCustomers();
        //System.out.println("test before adding "+list.size()+"      "+list2.size());
        for (int i = 0; i < list.size(); ++i)
        {
            if (list.get(i).getSaleLeadName().toLowerCase().contains(query.toLowerCase()))
                results.add(list.get(i).getSaleLeadName());
        }
        
        for (int i = 0; i < list2.size(); ++i)
        {
            if (list2.get(i).getSaleLeadName().toLowerCase().contains(query.toLowerCase()))
                results.add(list2.get(i).getSaleLeadName());
        }               
        return results;          
    }
    
    public void retrieve()
    {
        if (saleLeadName!=null)
        {
            this.setSelectedCustomer(customerBeanLocal.findCustomerByName(saleLeadName));
        }
    }
    
    public void makeCashInAdvance(ActionEvent event) throws IOException
    {
        System.out.println("AAAAAAAAAAAAa");    
        selectedSaleOrder = this.getSelectedSaleOrder();
        System.out.println("selected " + selectedSaleOrder);
        if (selectedSaleOrder != null)
        {
            if ( (selectedSaleOrder.getCashInAdvance()==null) || (selectedSaleOrder.getCashInAdvance().getCashInAdvanceAmount()==0) )
            {
                System.out.println("test1");
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedSaleOrder", selectedSaleOrder);
                FacesContext.getCurrentInstance().getExternalContext().redirect("../cashInAdvance/addCashInAdvance.xhtml");                            
            }
            else
            {
                System.out.println("test2");
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedSaleOrder", selectedSaleOrder);
                FacesContext.getCurrentInstance().getExternalContext().redirect("../cashInAdvance/editCashInAdvance.xhtml");                            
            }
        }
    }
    
    public void payExistingInvoice(ActionEvent event) throws IOException
    {
        selectedCustomer = this.getSelectedCustomer();        
        if (selectedCustomer != null){
            System.out.println("payExistinginvoice test"+selectedCustomer.getSaleLeadName());
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedCustomer", selectedCustomer);
            FacesContext.getCurrentInstance().getExternalContext().redirect("../customerInvoice/viewCustomerInvoicesPending.xhtml");                
        }
    } 
    
    public void reduceOrder(ActionEvent event) throws IOException
    {
        SaleOrder selectedSaleOrder = this.getSelectedSaleOrder();        
        if (selectedSaleOrder != null){
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedSaleOrder", selectedSaleOrder);
            FacesContext.getCurrentInstance().getExternalContext().redirect("../saleorder/editSaleOrder.xhtml");                
        }
    }
    
    public void refund(ActionEvent event) throws IOException
    {
        SaleOrder selectedSaleOrder = this.getSelectedSaleOrder();        
        if (selectedSaleOrder != null){
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedSaleOrder", selectedSaleOrder);
            FacesContext.getCurrentInstance().getExternalContext().redirect("../cashInAdvance/refundCashInAdvance.xhtml");                
        }
    }
}
